'use strict';
$(function () {
    $.ajaxSetup({
        headers:{
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        xhrFields:{withCredentials:true}
    })
});

function Ajax(params, method) {
    if (!params || !params.url) {
        console.log('ajax 方法配置失败，params必传参数 url');
    }
    var ajaxJson = {
        type: 'GET',
        url: params.url,
        dataType:'json',
        success:function (data) {
            if (params.result) return params.result(data);
            
            if (data.errCode === 0) {
                if (params.success) {
                    var resultData  = data.data? data.data:null;
                    params.success(resultData)
                }
            } else {
                try {
                    top.toastr.warning(data.errMsg);
                } catch (e) {
                    console.log(data.errMsg);
                    console.log(e.message)
                }
            }
        },
        error:function (jqXHR, textStatus, errorThrown) {
            console.log('textStatus: ' + textStatus);
            console.log('errorThrown: ' + errorThrown);
            console.log(jqXHR);
            console.log(jqXHR.responseText);
            try {
                top.toastr.error(jqXHR.status, '服务器响应失败;')
            } catch (e) {
                console.log(e)
            }
            if (params.error) {
                params.error(data.data);
            }
        },
        complete:function () {
            if (params.complete) {
                params.complete()
            }
        }
    }

    if (method) {
        ajaxJson.type = method;
    }

    if (params.data) {
        ajaxJson.data = params.data;
    }

    if (params.header) {
        ajaxJson.headers = params.header;
    }

    $.ajax(ajaxJson);
}

function AjaxGet(params) {
    return Ajax(params, 'GET')
}

function AjaxPost(params) {
    return Ajax(params, 'POST')
}

function AjaxPut(params) {
    return Ajax(params, 'PUT')
}

function AjaxDelete(params) {
    return Ajax(params, 'DELETE')
}

function AjaxFormSubmit(selector, success, before) {
    $(selector).find('input').on('keydown', function (e) {
        if (e.keyCode===13) {
            return _ajaxFormSubmit(selector, $(selector).find('.ajax-submit'), success, before)
        }
    });
    $(selector).find('.ajax-submit').on('click', function (e) {
        e.preventDefault();
        return _ajaxFormSubmit(selector, $(this), success, before)
    })
}

function _ajaxFormSubmit(selector, current_btn, success, before) {
    var load_content = '<i class="layui-icon layui-anim layui-anim-rotate layui-anim-loop"></i>';
    var default_content = current_btn.html()
    current_btn.html(load_content);
    current_btn.attr('disabled', true);
    current_btn.addClass('layui-btn-disabled');

    if (before) before();

    $(selector).ajaxSubmit({
        dataType:'json',
        success:function (data) {
            if (data.errCode === 0) {
                if (success) return success(data.data);
            } else {
                try {
                    top.toastr.warning(data.errMsg);
                } catch (e) {
                    console.log(data.errMsg);
                    console.log(e.message);
                }
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log("textStatus:" + textStatus);
            console.log("errorThrown:" + errorThrown);
            console.log(jqXHR);
            console.log(jqXHR.responseText);
            try {
                toastr.error('服务器响应失败;');
            } catch (e) {
                console.log('toastr 提醒报错');
            }
        },
        complete: function () {
            current_btn.html(default_content);
            current_btn.attr('disabled', false);
            current_btn.removeClass('layui-btn-disabled');
        }
    })

}